package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import io.renren.modules.sys.dao.SysRechargeDao;
import io.renren.modules.sys.entity.SysRechargeEntity;
import io.renren.modules.sys.service.SysRechargeService;



@Service("sysRechargeService")
public class SysRechargeServiceImpl implements SysRechargeService {

	@Autowired
	private SysRechargeDao sysRechargeDao;

	@Autowired
	private SysUserService sysUserService;
	
	@Override
	public SysRechargeEntity queryObject(Long rechargeId){
		return sysRechargeDao.queryObject(rechargeId);
	}
	
	@Override
	public List<SysRechargeEntity> queryList(Map<String, Object> map){
		return sysRechargeDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return sysRechargeDao.queryTotal(map);
	}
	
	@Override
	public void save(SysRechargeEntity sysRecharge){
		if (sysUserService.userExtract(sysRecharge.getUserId(), sysRecharge.getAmount() * 100)) {
			sysRechargeDao.save(sysRecharge);
		}
	}
	
	@Override
	public void update(SysRechargeEntity sysRecharge){
		sysRechargeDao.update(sysRecharge);
	}
	
	@Override
	public void delete(Long rechargeId){
		SysRechargeEntity rechargeEntity = sysRechargeDao.queryObject(rechargeId);

		if (rechargeEntity != null) {
			if (sysUserService.userExtract(rechargeEntity.getUserId(), -rechargeEntity.getAmount())) {
				sysRechargeDao.delete(rechargeId);
			}
		}
	}
	
	@Override
	public void deleteBatch(Long[] rechargeIds){
		for (Long rechargeId : rechargeIds) {
			delete(rechargeId);
		}
	}
	
}
